package com.surelution.tinyerp

class FeeListController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [feeListInstanceList: FeeList.list(params), feeListInstanceTotal: FeeList.count()]
    }

    def create = {
        def feeListInstance = new FeeList()
        feeListInstance.properties = params
        return [feeListInstance: feeListInstance]
    }

    def save = {
        def feeListInstance = new FeeList(params)
        if (feeListInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'feeList.label', default: 'FeeList'), feeListInstance.id])}"
            redirect(action: "show", id: feeListInstance.id)
        }
        else {
            render(view: "create", model: [feeListInstance: feeListInstance])
        }
    }

    def show = {
        def feeListInstance = FeeList.get(params.id)
        if (!feeListInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'feeList.label', default: 'FeeList'), params.id])}"
            redirect(action: "list")
        }
        else {
            [feeListInstance: feeListInstance]
        }
    }

    def edit = {
        def feeListInstance = FeeList.get(params.id)
        if (!feeListInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'feeList.label', default: 'FeeList'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [feeListInstance: feeListInstance]
        }
    }

    def update = {
        def feeListInstance = FeeList.get(params.id)
        if (feeListInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (feeListInstance.version > version) {
                    
                    feeListInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'feeList.label', default: 'FeeList')] as Object[], "Another user has updated this FeeList while you were editing")
                    render(view: "edit", model: [feeListInstance: feeListInstance])
                    return
                }
            }
            feeListInstance.properties = params
            if (!feeListInstance.hasErrors() && feeListInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'feeList.label', default: 'FeeList'), feeListInstance.id])}"
                redirect(action: "show", id: feeListInstance.id)
            }
            else {
                render(view: "edit", model: [feeListInstance: feeListInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'feeList.label', default: 'FeeList'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def feeListInstance = FeeList.get(params.id)
        if (feeListInstance) {
            try {
                feeListInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'feeList.label', default: 'FeeList'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'feeList.label', default: 'FeeList'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'feeList.label', default: 'FeeList'), params.id])}"
            redirect(action: "list")
        }
    }
	
	
	def loadAllFeeType = {
		def feeCategory = FeeTypeCategory.get(params.feeTypeCategoryId)
		def types
		if(feeCategory)
			types = FeeType.findAllByFeeTypeCategory(feeCategory)
		render(contentType:"text/xml") {
			category("id":feeCategory.id, "name":feeCategory.name) {
				types.each {t->
					type("id":t.id, "typeName":t.name, "category":t.feeTypeCategory.name)
				}
			}
		
		}
	}
	
	def feeTotal = {
		def result
		def startDay = new Date()
		def endDay = new Date()
		def types = []
		def selectType
		def feeTypeList = FeeTypeCategory.list()
		
		if (params.startDay&& params.endDay&&params.selectType){
			startDay = params.startDay
			endDay = params.endDay
			selectType = params.selectType
			if(selectType instanceof String) {
				types.add(Long.parseLong(selectType))
			} else {
				selectType.each {id->
					types.add(Long.parseLong(id))
				}
			}
			result = feeSearch(startDay,endDay,types)
			if (result==0){
				flash.message = "${message(code: 'default.nofound.message')}"
				redirect(action:"feeTotal")
			}
		}
		[startDay:startDay,endDay:endDay,result:result,selectType:types,feeList:feeTypeList]
	}
	
	def feeSearch(startDay,endDay,types){
		def syb = new StringBuilder("select fl.feeTime,fl.feeTypeCategory,fl.feeType,fl.saleMoney from FeeList fl where fl.feeTime <= :endDay and fl.feeTime >= :startDay and fl.feeTypeCategory.id in(:selectType)")
		def result = FeeList.executeQuery(syb.toString(),['startDay':startDay, 'endDay':endDay,'selectType':types])
		println result
		return result
	}
}
